package com.bishe.demo.modules.hotel.hotelDetail.serivce.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.hotel.common.dao.HotelImageDao;
import com.bishe.demo.modules.hotel.common.entity.HotelImage;
import com.bishe.demo.modules.hotel.hotelDetail.dao.DetailDao;
import com.bishe.demo.modules.hotel.hotelDetail.dao.DetailHotelImageDao;
import com.bishe.demo.modules.hotel.hotelDetail.dao.HotelDetailUserDao;
import com.bishe.demo.modules.hotel.hotelDetail.entity.Detail;
import com.bishe.demo.modules.hotel.hotelDetail.entity.DetailHotelImage;
import com.bishe.demo.modules.hotel.hotelDetail.entity.HotelDetailUser;
import com.bishe.demo.modules.hotel.hotelDetail.serivce.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private DetailDao detailDao;

    @Autowired
    private DetailHotelImageDao detailHotelImageDao;

    @Autowired
    private HotelImageDao imageDao;

    @Autowired
    private HotelDetailUserDao hotelDetailUserDao;

    @Override
    @Transactional
    public ResultEntity<Detail> insertDetail(Detail detail) {
        Detail detailByUserId = detailDao.getDetailByUserId(detail.getUser().getUserId());
        if (detailByUserId == null) {
            detail.setDetailId(detail.getUser().getUserId());
            detailDao.insertDetail(detail);
            insertImage(detail);
            hotelDetailUserDao.insertHotelDetailUser(new HotelDetailUser(detail.getDetailId(), detail.getUser().getUserId()));
            return new ResultEntity<Detail>(ResultEntity.ResultStatus.SUCCESS.code, "添加酒店详情成功", detail);
        } else {
            return new ResultEntity<Detail>(ResultEntity.ResultStatus.FAILED.code, "已有酒店简介");
        }
    }

    @Override
    public List<Detail> getDetails() {
        return detailDao.getDetails();
    }

    @Override
    public Detail getDetailByHotelId(int hotelId) {
        return detailDao.getDetailByHotelId(hotelId);
    }

    @Override
    @Transactional
    public ResultEntity<Detail> updateDetail(Detail detail) {
        detailDao.updateDetail(detail);
        List<HotelImage> detailImages = detail.getDetailImages();
        for (HotelImage detailImage : detailImages) {
            imageDao.deleteImageByImageId(detailImage.getId());
        }
        detailHotelImageDao.deleteDetailImageByDetailId(detail.getDetailId());
        insertImage(detail);
        return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", detail);
    }

    @Override
    public void deleteDetailByHotelId(int hotelId) {
        Detail detailByHotelId = detailDao.getDetailByHotelId(hotelId);
        List<HotelImage> detailImages = detailByHotelId.getDetailImages();
        for (HotelImage detailImage : detailImages) {
            imageDao.deleteImageByImageId(detailImage.getId());
        }
        detailHotelImageDao.deleteDetailImageByDetailId(hotelId);
        detailDao.deleteDetailByHotelId(hotelId);
        hotelDetailUserDao.deleteByUserId(hotelId);
    }


    @Transactional
    public void insertImage(Detail detail) {
        List<HotelImage> detailImages = detail.getDetailImages();
        if (!detailImages.isEmpty()) {
            for (HotelImage detailImage : detailImages) {
                imageDao.insertImage(detailImage);
                detailHotelImageDao.insertDetailHotelImage(new DetailHotelImage(detail.getDetailId(), detailImage.getId()));
            }
        } else {
            detailHotelImageDao.insertDetailHotelImage(new DetailHotelImage(detail.getDetailId(), 0));
        }
    }


}
